//音频资源
// import resourecs from './musicResourecs.js';
// let musics = resourecs.musicResourecs;
import http from '@/common/request.js';

let audio = null;
let timeout = null;
export default {
	state: {
		playStatus: false, //播放与暂停标识符
		curPlayIndex: 0, //当前播放的索引,
		durationTime: 0, //音频总时长
		currentTime: 0, //音频播放时刻
		audioList: [],
		musics: [{id: 1, name: '', src: '', singer: {name: '', synopsis: ''}}],
	},
	getters: {
		//音频名称
		audioName(state) {
			const curIndex = state.curPlayIndex;
			return state.musics[curIndex].name;
		},
		//歌手名称
		singerName(state) {
			const curIndex = state.curPlayIndex;
			const singer = state.musics[curIndex].singer;
			return singer.name;
		},
		//歌手简介
		singerSynopsis(state) {
			const curIndex = state.curPlayIndex;
			const singer = state.musics[curIndex].singer;
			return singer.synopsis;
		},
		curCover(state) {
			const curIndex = state.curPlayIndex;
			return state.musics[curIndex].cover;
		}
	},
	mutations: {
		// 销毁
		destruction() {
			audio.offPlay();
			audio.offPause();
			audio.offStop();
			audio.offEnded();
			audio.offError();
		},
		//播放
		audioPlay(state) {
			const index = state.curPlayIndex;
			state.audioList[index].playStatus = 1;
			if(audio.src !== state.musics[index].src){
				audio.src = state.musics[index].src;
			}
			audio.play();
		},
		//跳转
		audioSeek(state, pos) {
			audio.seek(pos);
		},
		//暂停
		audioPause(state) {
			const index = state.curPlayIndex;
			state.audioList[index].playStatus = -1;
			audio.pause();
		},
		//停止
		audioStop(state) {
			const index = state.curPlayIndex;
			state.audioList[index].playStatus = 0;
			audio.stop();
		},
		//改变播放标识
		changePlayIndex(state, index) {
			state.curPlayIndex = index;
		},
		//改变播放与暂停状态
		changePlayStatus(state, status) {
			state.playStatus = status;
		},
		//获取总时长
		getDurationTime(state, time) {
			state.durationTime = time;
		},
		//改变当前时间(暂停时)
		changeCurrentTime(state, time) {
			state.currentTime = time;
		},
		//获取音频列表
		getAudioList(state, audioList) {
			for (let item of audioList) {
				state.audioList.push({
					id: item.id,
					audioName: item.name,
					singerName: item.singer.name,
					playStatus: 0 // -1 -> 暂停 | 0 -> 停止 | 1 -> 播放
				})
			}
		}
	},
	actions: {
		async init({ state, commit, dispatch }) {
			if (audio) {
				return;
			}
			let {musicResourecs} = await http.get('/musicResourecs');
			state.musics = musicResourecs;
			
			audio = uni.createInnerAudioContext(); //实例化audio对象
			// #ifdef H5
			audio.src = state.musics[0].src;
			// #endif
			commit('getAudioList', state.musics);
			//音频的各种监听写在这里
			
			//监听
			audio.onCanplay(() => {
				//微信小程序首次播放不走onTimeUpdate故拿不到时长信息，需要放到这里监听一下
				// #ifdef MP-WEIXIN
				commit('getDurationTime', audio.duration);
				commit('changeCurrentTime', audio.currentTime);
				// #endif
			})
			//播放事件
			audio.onPlay(() => {
				commit('changePlayStatus', true);
				console.log('开始播放');
			})
			//暂停事件
			audio.onPause(() => {
				commit('changePlayStatus', false);
				console.log('暂停播放');
			})
			//停止事件
			audio.onStop(() => {
				commit('changePlayStatus', false);
				console.log('停止播放');
			})
			//播放结束事件
			audio.onEnded(() => {
				commit('changePlayStatus', false);
				dispatch('PreOrNext', 'next');
				console.log('播放结束');
			})
			//播放错误事件
			audio.onError((res) => {
				commit('changePlayStatus', false);
				console.log(res.errMsg);
				console.log(res.errCode);
			})
			//音频播放进度更新事件
			audio.onTimeUpdate(() => {
				commit('getDurationTime', audio.duration);
				commit('changeCurrentTime', audio.currentTime);
			})
		},
		PlayOrPause({ state, commit }) {
			if (!state.playStatus) {
				commit('audioPlay');
			} else {
				commit('audioPause');
			}
		},
		PreOrNext({
			state,
			commit
		}, type) {
			commit('audioStop');
			const curIndex = state.curPlayIndex;
			const lastIndex = state.musics.length - 1;
			switch (type) {
				case 'pre':
					curIndex === 0 ? commit('changePlayIndex', lastIndex) : commit('changePlayIndex', curIndex - 1)
					break;
				case 'next':
					curIndex === lastIndex ? commit('changePlayIndex', 0) : commit('changePlayIndex', curIndex + 1)
					break;
				default:
					break;
			}
			commit('audioPlay');
		},
		SlideToPlay({ state, commit }, { detail: { value: position } }) {
			let time = position;
			commit('audioSeek', time);
			if (!state.playStatus) {
				timeout && clearTimeout(timeout)
				timeout = setTimeout(() => commit('changeCurrentTime', time), 200)
			}
		},
		//列表选择播放
		selectPlay({ state, commit }, id){
			const index = state.musics.findIndex(item => item.id === id);
			if(state.curPlayIndex === index){
				if(state.playStatus){
					commit("audioPause");
				}else{
					commit("audioPlay");
				}
				return;
			}else{
				commit("audioStop");
			}
			commit("changePlayIndex", index);
			commit("audioPlay");
		}
	}
}
